package com.ldp.designpatterns.study.flyweight.factory;

import com.ldp.designpatterns.study.flyweight.flyAbstract.Chess;
import com.ldp.designpatterns.study.flyweight.impl.BlackChess;
import com.ldp.designpatterns.study.flyweight.impl.WhiteChess;

/**
 * @ClassName ChessFactory
 * @Description TODO:享元模式通过一个享元工厂（Flyweight Factory）来管理和创建共享对象。当需要一个对象时，工厂会检查是否已经有相同内部状态的对象存在，如果存在则返回已有的对象，否则创建一个新的对象并将其添加到内部对象池中。
 * @Author lindipei
 * @Date 2023/10/17 17:01
 */
public class ChessFactory {
    private static Chess whiteChess;
    private static Chess blackChess;
    public static Chess getChess(int key) {
        if (key == 0) {
            if (whiteChess == null) {
                System.out.println("=====创建白棋对象======");
                whiteChess = new WhiteChess();
            }
            return whiteChess;
        } else {
            if (blackChess == null) {
                System.out.println("=====创建黑棋对象======");
                blackChess = new BlackChess();
            }
            return blackChess;
        }
    }
}
